package cn.tedu.javagame.ball;

import cn.tedu.javagame.star.StarFrame;

import java.awt.*;

public class Ball {
    //属性
    int x,y;
    int length = 20;
    Color color;
    int speed_x,speed_y;
    int state = 0;//0左上角，1右上角，2左下角，3右下角
    public Ball(){
        this.x = (int) (Math.random() * (BallFrame.WIDTH - this.length + 1));
        this.y = (int) (Math.random() * (BallFrame.HEIGHT - this.length + 1));//随机数
        this.color = new Color((int) (Math.random() * (256)),(int)( Math.random() * (256)),(int)( Math.random() * (256)));
//        this.r = (int) (Math.random() * 80 + 20);
        this.speed_x = (int) (Math.random() * 6 + 2);
        this.speed_y = this.speed_x;
        this.state = (int)(Math.random() *4 + 0);

    }
    public void move(){

        if (this.y <= 0) {
            this.y = 0; // 确保弹球不会超出上边界
            this.speed_y = -this.speed_y;
        } else if (this.y + this.length >= BallFrame.HEIGHT) {
            this.y = BallFrame.HEIGHT - this.length; // 确保弹球不会超出下边界
            this.speed_y = -this.speed_y;
        }

        if (this.x <= 0) {
            this.x = 0; // 确保弹球不会超出左边界
            this.speed_x = -this.speed_x;
        } else if (this.x + this.length >= BallFrame.WIDTH) {
            this.x = BallFrame.WIDTH - this.length; // 确保弹球不会超出右边界
            this.speed_x = -this.speed_x;
        }

        if (this.state == 0){
            this.y -= this.speed_y;
            this.x -= this.speed_x;
        }else if(this.state == 1){
            this.y -= this.speed_y;
            this.x += this.speed_x;
        }else if(this.state == 2){
            this.y += this.speed_y;
            this.x -= this.speed_x;
        }else{
            this.y += this.speed_y;
            this.x += this.speed_x;
        }

    }
    public boolean hitBrick(){
        return true;
    }
}
